import Api from '@/api/Axios.js'

var orderList = (statu = 0) => {
  return Api.post('data/order/orderList.json').then((data) => {
    var order = []
    var orderSet = data.data.filter(function (order) {
      return order.member === 1
    })
    if (statu !== 0) {
      orderSet = orderSet.filter(function (value) {
        return value.status === statu
      })
    }
    for (let _order of orderSet) {
      var goodObj = {
        image: _order.image,
        name: _order.name,
        specification () {
          var specifications = ''
          for (let specification of _order.specification_values) {
            specifications += specification.name + '：' + specification.value + '  '
          }
          return specifications
        },
        price: _order.price,
        freight: _order.freight,
        num: _order.num,
        store: _order.store,
        status: _order.status,
        id: _order.id,
        sn: _order.sn,
        createDate: _order.createDate,
        receptionName: _order.receptionName,
        receptionPhone: _order.receptionPhone,
        receptionArea: _order.receptionArea,
        receptionAddress: _order.receptionAddress,
        isDefault: _order.isDefault
      }

      order.push(goodObj)
    }
    return order
  })
}

const couponCode = (num = 0) => { // 优惠劵
  return Api.post('data/coupon/couponCode.json').then((data) => {
    var coupon = []
    var couponSet = data.data
    if (num !== 0) {
      couponSet = data.data.filter(function (val) {
        return val.member === 2 // 好像有点问题
      })
    }
    for (let couponData of couponSet) {
      var couponObj = {
        price: couponData.price,
        title: couponData.title,
        date: couponData.date,
        content: couponData.content,
        name: couponData.name
      }
      coupon.push(couponObj)
    }
    return coupon
  })
}

var refund = () => { // 退款
  return Api.post('data/order/refund.json').then((data) => {
    var refundList = []
    var refundSet = data.data.filter(function (order) {
      return order.member === 1
    })
    refundSet = refundSet.filter(function (value) {
      return value.status > 4
    })
    for (let _refund of refundSet) {
      var refundObj = {
        image: _refund.image,
        name: _refund.name,
        shopName: _refund.store,
        specification () {
          var specifications = ''
          for (let specification of _refund.specification_values) {
            specifications += specification.name + '：' + specification.value + '  '
          }
          return specifications
        },
        price: _refund.price,
        status: _refund.status,
        id: _refund.id
      }
      refundList.push(refundObj)
    }
    return refundList
  })
}

const classification = () => { // 分类
  return Api.post('data/goods/category.json').then((data) => {
    const categoryData = data.data
    const len = categoryData.length
    var categoryAry = []
    for (let i = 0; i < len; i++) {
      var classificationObj = {
        id: categoryData[i].id,
        name: categoryData[i].name
      }
      categoryAry.push(classificationObj)
    }
    return categoryAry
  })
}
const sonClassification = (val = 1) => { // 子分类
  return Api.post('data/goods/soncategory.json').then((data) => {
    const categoryData = data.data.filter((value) => {
      return value.pid === val
    })
    const len = categoryData.length
    var categoryAry = []
    for (let i = 0; i < len; i++) {
      var classificationObj = {
        name: categoryData[i].name
      }
      classificationObj.goodList = []
      const sonLen = categoryData[i].children.length
      for (let j = 0; j < sonLen; j++) {
        var sonClassificationObj = {
          id: categoryData[i].children[j].id,
          name: categoryData[i].children[j].name,
          image: categoryData[i].children[j].image
        }
        classificationObj.goodList.push(sonClassificationObj)
      }
      categoryAry.push(classificationObj)
    }
    return categoryAry
  })
}

const shoppingCart = () => { // 购物车
  return Api.post('data/order/orderList.json').then((data) => {
    var shoppingCart = []
    const goodList = data.data
    var shopSet = new Set()
    for (let i = 0; i < goodList.length; i++) {
      shopSet.add(goodList[i].store)
    }
    for (let i of shopSet) {
      var shoppingCartObj = {
        store: i,
        checkStoreAll: false
      }
      shoppingCartObj.goods = []
      for (let j = 0; j < goodList.length; j++) {
        if (goodList[j].store === i) {
          var goodObj = {
            img: goodList[j].image,
            name: goodList[j].name,
            color: goodList[j].specification_values[0].value,
            size: goodList[j].specification_values[1].value,
            price: goodList[j].price,
            num: goodList[j].num,
            check: false
          }
          shoppingCartObj.goods.push(goodObj)
        }
      }
      shoppingCart.push(shoppingCartObj)
    }
    return shoppingCart
  })
}

const goodDetail = () => {
  return Api.post('data/goods/goods.json').then((data) => {
    const goodDetail = data.data[0]
    var goodDetailObj = {
      shopName: goodDetail.shop,
      goodName: goodDetail.name,
      goodTip: goodDetail.tip,
      goodPrice: goodDetail.price,
      goodOldPrice: goodDetail.marketPrice,
      goodWeight: goodDetail.weight
    }
    goodDetailObj.swiperList = []
    for (let i = 0; i < goodDetail.slideshow.length; i++) {
      var imgObj = {}
      imgObj.img = goodDetail.slideshow[i]
      goodDetailObj.swiperList.push(imgObj)
    }
    return goodDetailObj
  })
}

const popup = () => {
  return Api.post('data/goods/goods.json').then((data) => {
    var goods = data.data.filter(function (popupObj) {
      return popupObj.id === 1
    })
    return goods[0]
  })
}

const address = (orderId = 0) => { // 收货地址
  return Api.post('data/member/address.json').then((data) => {
    var addressArr = []
    var addressData = data.data.filter(function (val) {
      return val.member === 1
    })
    if (orderId !== 0) {
      addressData = addressData.filter(function (value) {
        return value.id === orderId
      })
    }
    for (let i = 0; i < addressData.length; i++) {
      // 登入ID过滤
      var addressObj = {
        name: addressData[i].name,
        phone: addressData[i].phone,
        placeA: addressData[i].area,
        placeB: addressData[i].address,
        isDefault: addressData[i].isDefault
      }
      addressArr.push(addressObj)
    }
    return addressArr
  })
}
const specification = () => { // 数据规格
  return Api.post('data/goods/product.json').then((data) => {
    var shopData = data.data.filter((value) => {
      return value.shop === 1
    })
    var specificationObj = {
      specLength: shopData[0].specification_values.length
    }
    for (let i = 0; i < shopData.length; i++) {
      var key = shopData[i].specification_values[0].value + ',' + shopData[i].specification_values[1].value
      specificationObj[key] = 'true'
    }
    return specificationObj
  })
}

const refundDetail = () => {
  return Api.post('data/order/refundDetails.json').then((data) => {
    var refundDetails = data.data.filter(function (value) {
      return value.id === 1
    })
    refundDetails[0].specification =
      refundDetails[0].specification_values[0].name + ':' +
      refundDetails[0].specification_values[0].value + ',' +
      refundDetails[0].specification_values[1].name + ':' +
      refundDetails[0].specification_values[1].value
    var list = []
    for (var i = 0; i < 5; i++) {
      var refund_ = {}
      var newrefund = [
        '退款金额: ￥' + refundDetails[0].price,
        '退款金额: ￥' + refundDetails[0].price,
        '申请件数:' + refundDetails[0].quantity,
        '申请时间:' + refundDetails[0].createDate,
        '退款编号:' + refundDetails[0].sn
      ]
      refund_.label = newrefund[i]
      list.push(refund_)
    }
    refundDetails[0].list = list
    return refundDetails[0]
  })
}

const userInfo = () => {
  return Api.post('data/member/member.json').then((data) => {
    const userInfo_ = data.data.filter(function (value) {
      return value.id === 1
    })
    const userInfoData = {
      image: userInfo_[0].headimg,
      username: userInfo_[0].nickname,
      birthday: userInfo_[0].birthday
    }
    return userInfoData
  })
}

const swiper = () => { // 轮播图
  return Api.post('data/index/swiper.json').then((data) => {
    const swiperImg = data.data
    return swiperImg
  })
}
const letters = () => {
  return Api.post('data/index/letters.json').then((data) => {
    const letters = data.data
    return letters
  })
}
const fourAdvertising = () => {
  return Api.post('data/index/fourAdvertising.json').then((data) => {
    const fourAdvertising = data.data
    return fourAdvertising
  })
}
const rectangularAdvertising = () => {
  return Api.post('data/index/rectangularAdvertising.json').then((data) => {
    const rectangularAdvertising = data.data
    return rectangularAdvertising
  })
}

const goodList = (val = 700) => { // 商品列表--默认是为你推荐的内容
  return Api.post('data/goods/goods.json').then((data) => {
    const categoryData = data.data.filter((value) => {
      return value.category === val
    })
    const goodListData = []
    const len = categoryData.length
    for (let i = 0; i < len; i++) {
      var good = {
        img: categoryData[i].image,
        goodName: categoryData[i].name,
        goodPrice: categoryData[i].price
      }
      goodListData.push(good)
    }
    return goodListData
  })
}

export default {
  address: address,
  classification: classification(),
  shoppingCart: shoppingCart(),
  sonClassification: sonClassification,
  goodDetail: goodDetail(),
  popup: popup(),
  orderList: orderList,
  refund: refund(),
  specification: specification(),
  refundDetail: refundDetail(),
  userInfo: userInfo(),
  swiper: swiper(),
  fourAdvertising: fourAdvertising(),
  rectangularAdvertising: rectangularAdvertising(),
  letters: letters(),
  goodList: goodList,
  couponCode: couponCode
}
